__author__ = 'sdjkx'
try:
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
except ImportError:
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *

import hashlib

from libs.utils import newIcon
from libs.Config import Config

BB = QDialogButtonBox

class LoginDialog(QDialog):
    def __init__(self, parent=None,mydb = None, userIdStr=None ):
        super(LoginDialog, self).__init__(parent)

        self.setWindowTitle("登录")
        self.mydb = mydb

        self.layout = QVBoxLayout()
        self.userId = 0
        self.userName = ""

        self.lbl_name = QLabel()
        # self.lbl_name.setGeometry(QRect(40, 30, 72, 15))
        self.lbl_name.setObjectName("lbl_name")
        self.lbl_name.setText("帐号：")
        self.layout.addWidget(self.lbl_name)

        self.edit_name = QLineEdit()
        # self.edit_name.setGeometry(QRect(130, 30, 221, 21))
        self.edit_name.setObjectName("edit_name")
        self.edit_name.setText(userIdStr)
        # self.edit_name.setValidator(self.editValidator())
        # self.edit_name.editingFinished.connect(self.editValidator)
        self.layout.addWidget(self.edit_name)

        self.lbl_password = QLabel()
        self.lbl_password.setObjectName("lbl_password")
        self.lbl_password.setText("密码：")
        self.layout.addWidget(self.lbl_password)

        self.edit_password = QLineEdit()
        self.edit_password.setObjectName("edit_password")
        self.edit_password.setEchoMode(QLineEdit.Password)
        # self.edit_password.setValidator(self.editValidator())
        # self.edit_password.editingFinished.connect(self.editValidator)
        self.layout.addWidget(self.edit_password)

        self.buttonBox = bb = BB(BB.Ok | BB.Cancel, Qt.Horizontal, self)
        bb.button(BB.Ok).setIcon(newIcon('done'))
        bb.button(BB.Cancel).setIcon(newIcon('undo'))
        bb.accepted.connect(self.validate)
        bb.rejected.connect(self.reject)
        self.layout.addWidget(bb)

        self.setLayout(self.layout)
        self.edit_name.setFocus()

    def validate(self):
        name = self.edit_name.text()
        password = self.edit_password.text()

        if len(name)> 0 and len(password) > 0:
            if self.loginServer(name, password):
                self.accept()
        else:
             QMessageBox.information(self, u'提示', "请输入帐号和密码！")

    def loginServer(self, name, password):
        # 创建md5对象，
        # md5对象，md5不能反解，但是加密是固定的，就是关系是一一对应，所以有缺陷，可以被对撞出来
        hl = hashlib.md5()

        hl.update(password.encode(encoding='utf-8'))
        # print('MD5加密前为 ：' + password)
        # hl.hexdigest()) #拿到加密字符串
        password = hl.hexdigest()
        # print('MD5加密后为 ：' + password)

        sql = "select userid, `password` from sys_labelImg_user where userName='"+name+"'"
        results = self.mydb.executeSql(sql)
        if len(results) > 0:
            for row in results:
                if row[1] != password:
                    QMessageBox.information(self, u'提示', "密码错误")
                else:
                    self.userId = row[0]
                    self.userName = name
                    return True
        else:
            QMessageBox.information(self, u'提示', "帐号错误")
        return False


    @staticmethod
    def login(parent=None, mydb = None, userIdStr= None):
        dialog = LoginDialog(parent, mydb, userIdStr)
        dialog.exec_()
        userId = dialog.userId
        name = dialog.userName
        return userId, name